package com.pig.config;

import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@Slf4j
public class Test implements GlobalFilter  {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.error(exchange.toString());
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();

        String authorization1 = headers.getFirst("Authorization");
        String substring=null;
if (authorization1!=null) {
      authorization1.substring(7);
}


        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();

        log.error("过滤器获取到的令牌为\n"+substring);
//request.mutate().header("Authorization", "123").build();


        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(request);
        log.error( "*****************");
         authorization1 = headers.getFirst("Authorization");
        System.out.println("当前令牌为\n"+authorization1);
        return chain.filter(exchange.mutate().request(request).build());
    }
}
